
var userinfoListIns, $ ,currPage = 1,totalCount,detaillayero;

layui.use(['layer','form', 'laydate','table','element'], function() {

    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;

    /*基本组件渲染*/

    userinfoListIns = table.render({
        elem: '#userinfoList' //初始化使用dom id
        ,url : '/fsm/userinfo/queryUserinfoList'
        ,request: {
            pageName: 'page' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,statusCode: 1 //成功的状态码，默认：0
            ,msgName: 'msg' //状态信息的字段名称，默认：msg
            ,countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'records' //数据列表的字段名称，默认：data
        }
        ,loading: true
//            ,height: 'full-200' //高度最大化减去差值
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[ //标题栏
            {type:'checkbox'}
            ,{field: 'username',align:'center', title: '用户名', width: 160,sort: true}
            ,{field: 'name', title: '姓名', width: 160,align:'center'}
            ,{field: 'enrollNumber', title: '工号',width:120,align:'center'}
            ,{field:'right', title: '操作', width:177,toolbar:"#barEdit"}
            ,{field: 'id',style:'display: none'}
        ]]
//           ,skin: 'line' //表格风格
//        ,even: true
        ,page: {
            limit: 10
            ,limits: [10, 20, 30, 40, 50]
            ,curr: 1
            ,groups: 5
            ,theme: '#5FB878'
            ,first: '首页'
            ,last: '尾页'
            ,prev: '<em><i class="layui-icon">&#xe603;</i></em>'
            ,next: '<em><i class="layui-icon">&#xe602;</i></em>'
            ,layout: [ 'prev', 'page', 'next', 'skip','count', 'limit']
        }/*,done: function(res, curr, count){
                layer.close(load);
                loadDataCallBack(res, curr, count);
            },where:whereData*/
    });

    table.on('tool(userinfoList)', function(obj){
        var load = layer.load();
        var data = obj.data;

        if(obj.event === 'del'){
            layer.confirm('真的删除该记录么?', {btn: ['是','否'],
                btn1: function (index) {
                    var load = layer.load();
                    $.ajax({
                        type: "post",
                        async: true,
                        data: {userinfoId: data.id},
                        url: "/fsm/userinfo/delUserinfoById",
                        success: function (data) {
                            if (data.code == 1) {
                                layer.msg(data.msg, {
                                    icon: 1,
                                    time: 1500 //2秒关闭（如果不配置，默认是3秒）
                                }, function () {
                                    //window.location.reload();
                                    var button = $("#searchBtn");
                                    button.click();
                                });
                            } else {
                                layer.msg(data.msg)
                            }
                            layer.close(load);
                        },
                        error: function () {
                            layer.msg("删除操作异常");
                            layer.close(load);
                        }
                    })
                    layer.close(index);
                },btn2: function() {
                    layer.close(load);
                },cancel: function() {
                    layer.close(load);
                }
            }
        );
        } else if(obj.event === 'edit'){
            layer.open({
                type: 2,
                title: '编辑用户',
                // title:data.to,
                shadeClose: true,
                shade: 0.8,
                area: ['400px', '270px'],
                content: '/fsm/userinfo/editUserinfo?userinfoId=' + data.id,
                end: function(){
                    var button = $("#searchBtn");
                    button.click();
                }
            });
            /*var button = $("#searchBtn").find("[lay-submit]");
            button.click();*/
        }
    });



    // 提交查询条件表单
    form.on('submit(query)', function (data) {

        var username = $("#username").val().trim();
        if(username != null && username !="") {
            data.field["username"] = username;
        }

        var name = $("#name").val().trim();
        if(name != null && name !="") {
            data.field["name"] = name;
        }

        var enrollNumber = $("#enrollNumber").val().trim();
        if(enrollNumber != null && enrollNumber !="") {
            data.field["enrollNumber"] = enrollNumber;
        }


        var load = layer.load();
        userinfoListIns.reload({
            url : '/fsm/userinfo/queryUserinfoList'
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: data.field
            ,done: function(res, curr, count){
                layer.close(load);
            }
        });

    });



    form.on('submit(addUserinfo)', function (data) {
            layer.open({
                type: 2,
                title: '添加用户',
                // title:data.to,
                shadeClose: true,
                shade: 0.8,
                area: ['400px', '370px'],
                content: '/fsm/userinfo/editUserinfo?userinfoId=0',
                end: function(){
                    var button = $("#searchBtn");
                    button.click();
                }
            });
    });

    form.on('submit(getCheckData)', function (data) {//获取选中数据
        var load = layer.load();
        var checkStatus = table.checkStatus('userinfoList');
        data = checkStatus.data;
        if (data.length <= 0) {
            layer.alert("请选择要删除的记录！")
            layer.close(load);
        } else {
            layer.confirm('删除选中的' + data.length + '条记录么?', {
                    btn: ['是', '否'],
                    btn1: function (index) {
                        var load = layer.load();
                        var ids = ''
                        for (var i = 0; i < data.length; i++) {
                            if (i == (data.length - 1)) {
                                ids += (data[i].id)
                            } else {
                                ids += (data[i].id + ",")
                            }

                        }
                        $.ajax({
                            type: "post",
                            async: true,
                            data: {ids: ids},
                            url: "/fsm/userinfo/delUserinfoByIds",
                            success: function (data) {
                                if (data.code == 1) {
                                    layer.msg(data.msg, {
                                        icon: 1,
                                        time: 1500 //2秒关闭（如果不配置，默认是3秒）
                                    }, function () {
                                        //window.location.reload();
                                        var button = $("#searchBtn");
                                        button.click();
                                    });
                                } else {
                                    layer.msg(data.msg)
                                }
                                layer.close(load);
                            },
                            error: function () {
                                layer.msg("删除操作异常");
                                layer.close(load);
                            }
                        })
                        layer.close(index);
                    }, btn2: function () {
                        layer.close(load);
                    }, cancel: function () {
                        layer.close(load);
                    }
                }
            );
        }
    });

});



